import axios, { AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'
import { baseURL } from '@/utils/config'

const instance = axios.create({
  withCredentials: true,
  baseURL
})

instance.interceptors.response.use((response: AxiosResponse<any>): AxiosResponse<any> | Promise<AxiosResponse<any>> => {
  return response
}, (error) => {
  ElMessage.error('服务器错误，请联系管理员')
  return error
})

export const getRequest = (url: string, params?: any) => {
  return instance({
    method: 'get',
    url: `${url}`,
    params
  })
}

export const postRequest = (url: string, data?: any) => {
  return instance({
    method: 'post',
    url: `${url}`,
    data
  })
}
